package com.example.anpuser.controller.crud;

import com.example.anpapi.entity.po.usermodel.UserStatistics.UserStatistics;
import com.example.anpapi.entity.vo.R;
import com.example.anpuser.service.interfaces.UserStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user-statistics")
public class UserStatisticsController {

    @Autowired
    private UserStatisticsService userStatisticsService;

    /**
     * 用户统计信息添加
     * @param userStatistics
     * @return
     */
    @PostMapping("/save")
    public R<UserStatistics> save(@RequestBody UserStatistics userStatistics){
        log.info("用户统计信息添加:{}", userStatistics);
        userStatisticsService.save(userStatistics);
        return R.success(userStatistics,"用户统计信息添加成功");
    }

    /**
     * 根据统计id删除用户统计信息
     * @param userStatistics
     * @return
     */
    @DeleteMapping("/remove")
    public R<String> remove(@RequestBody UserStatistics userStatistics){
        log.info("用户统计信息删除:{}", userStatistics);
        if(userStatisticsService.getById(userStatistics.getId()) != null)
        {
            userStatisticsService.removeById(userStatistics.getId());
            return R.success(null,"用户统计信息删除成功");
        }
        return R.error("未找到该用户统计信息");
    }

    /**
     * 根据统计id查找用户统计信息
     * @param id
     * @return
     */
    @GetMapping("/get")
    public R<UserStatistics> get(@RequestParam(value = "id") Long id) {
        log.info("查询用户统计信息的id为{}", id);
        UserStatistics userStatistics = userStatisticsService.getById(id);
        return R.success(userStatistics, "查询用户统计信息成功");
    }

    /**
     * 根据用户id查找用户统计信息
     * @param userId
     * @return
     */
    @GetMapping("/getByUserId")
    public R<UserStatistics> getByUserId(@RequestParam(value = "userId") Long userId) {
        log.info("查询用户统计信息的userId为{}", userId);
        UserStatistics userStatistics = userStatisticsService.lambdaQuery()
                .eq(UserStatistics::getUserId, userId)
                .one();
        return R.success(userStatistics, "查询用户统计信息成功");
    }

    /**
     * 修改用户统计信息
     * @param userStatistics
     * @return
     */
    @PostMapping("/update")
    public R<UserStatistics> update(@RequestBody UserStatistics userStatistics){
        log.info("修改的用户统计信息为{}", userStatistics);
        userStatisticsService.updateById(userStatistics);
        return R.success(userStatistics,"修改用户统计信息成功");
    }

}


